package main

import (
	"context"
	"fmt"
	"io/ioutil"
	"send-id-num/api"
	"send-id-num/config"
	"send-id-num/internal/repository"
	server "send-id-num/pkg/http"
	"send-id-num/pkg/logger"
	"send-id-num/pkg/signal"
	"strconv"
	"sync"
	"time"
)

func main() {

	var wg sync.WaitGroup
	wg.Add(1)
	routers := api.InitRouter()

	logger.Log.Info("http:", config.Cfg.Server.Http)
	app := server.NewHttpServer(&wg, ":"+strconv.Itoa(config.Cfg.Server.Http), routers)
	err := repository.Init()
	if err != nil {
		logger.Log.Error("Init err ", err)
		return
	}
	logger.Log.Info("app start complete...")
	signal.WaitForSignals()

	ctxShutdown, cancelShutdown := context.WithTimeout(context.Background(), 2*time.Second)
	defer cancelShutdown()
	_ = app.Shutdown(ctxShutdown)

	wg.Wait()
	logger.Log.Info("app exit complete...")
	return
}

func init() {
	banner, _ := ioutil.ReadFile("banner.txt")
	fmt.Println(string(banner))
}
